<template>
  <div class="settings">
    <van-nav-bar safe-area-inset-top placeholder :title="$t('Usersettings')" left-arrow fixed @click-left="onClickLeft" />
    <van-pull-refresh v-model="isLoading" :pulling-text="$t('PullingText')" :loosing-text="$t('PullingText')" :success-text="$t('refreshSuccee')" @refresh="onRefresh">
      <van-collapse class="marginTop15" v-model="activeNames">
        <van-collapse-item :title="$t('Datetime')" name="1">
          <datetimePTVue :times="LocalArr.datetimes"></datetimePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('Language')" name="2">
          <languagePTVue :columns="langArr" :setValue="LocalArr.lang" :reg="46"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('OffGridMute')" name="3">
          <languagePTVue :setName="$t('Mute')" :columns="toggleArr" :setValue="LocalArr.EPSMuteValue" :reg="49"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('SelfUse')" name="4">
          <pfLockInPointPTVue :setName="$t('MinSoc')" :setUntil="'%'" :setMin="10" :setMax="100" :setValue.sync="LocalArr.SelfUseMinSocValue" :reg="29"></pfLockInPointPTVue>
          <languagePTVue
            :setName="$t('ChargeAtNight')"
            :columns="backupModeArr"
            :reg="30"
            :setValue="LocalArr.ChargeAtNightCode"
            @getFormData="setChargeAtNight"
          ></languagePTVue>
          <pfLockInPointPTVue :setName="$t('ChargeNightUpperSoc')" :setUntil="'%'" :setMin="10" :setMax="100" :setValue.sync="LocalArr.ChargeNightUpperSocValue" :reg="31" v-if="isChargeAtNight"></pfLockInPointPTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('FeedInPriority')" name="5">
          <pfLockInPointPTVue :setName="$t('MinSoc')" :setUntil="'%'" :setMin="10" :setMax="100" :setValue.sync="LocalArr.FeedInMinSocValue" :reg="32"></pfLockInPointPTVue>
          <pfLockInPointPTVue :setName="$t('ChargeNightUpperSoc')" :setUntil="'%'" :setMin="10" :setMax="100" :setValue.sync="LocalArr.FeedInChargeNightUpperSocValue" :reg="33"></pfLockInPointPTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('BackupMode')" name="6">
          <pfLockInPointPTVue :setName="$t('MinSoc')" :setUntil="'%'" :setMin="30" :setMax="100" :setValue.sync="LocalArr.BackupMinSocValue" :reg="34"></pfLockInPointPTVue>
          <pfLockInPointPTVue :setName="$t('ChargeNightUpperSoc')" :setUntil="'%'" :setMin="30" :setMax="100" :setValue.sync="LocalArr.BackupChargeNightUpperSocValue" :reg="35"></pfLockInPointPTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('ChrgAndDischrgPeriod')" name="7">
          <chargeTimePT3Vue :setName="$t('ChargeStartTime')" :setValue="LocalArr.ChargeStartTime" :reg="37"></chargeTimePT3Vue>
          <chargeTimePT3Vue :setName="$t('ChargeEndTime')" :setValue="LocalArr.ChargeEndTime" :reg="38"></chargeTimePT3Vue>
          <chargeTimePT3Vue :setName="$t('DischargeStartTime')" :setValue="LocalArr.DisChargeStartTime" :reg="39"></chargeTimePT3Vue>
          <chargeTimePT3Vue :setName="$t('DischargeEndTime')" :setValue="LocalArr.DisChargeEndTime" :reg="40"></chargeTimePT3Vue>
          <van-divider>{{$t('ChrgAndDischrgPeriod2')}}</van-divider>
          <languagePTVue
            :setName="$t('Function')"
            :columns="backupModeArr"
            :reg="41"
            :setValue="LocalArr.FunctionCode"
            @getFormData="setFunction"
          ></languagePTVue>
          <chargeTimePT3Vue :setName="$t('ChargeStartTime')" :setValue="LocalArr.ChargeStartTime2" :reg="42" v-if="isFunction"></chargeTimePT3Vue>
          <chargeTimePT3Vue :setName="$t('ChargeEndTime')" :setValue="LocalArr.ChargeEndTime2" :reg="43" v-if="isFunction"></chargeTimePT3Vue>
          <chargeTimePT3Vue :setName="$t('DischargeStartTime')" :setValue="LocalArr.DisChargeStartTime2" :reg="44" v-if="isFunction"></chargeTimePT3Vue>
          <chargeTimePT3Vue :setName="$t('DischargeEndTime')" :setValue="LocalArr.DisChargeEndTime2" :reg="45" v-if="isFunction"></chargeTimePT3Vue>
        </van-collapse-item>
        <van-collapse-item :title="'Dry Contact'" name="8">
          <languagePTVue
            :setName="'Dry Contact'"
            :columns="DryContactArr"
            :reg="132"
            :setValue="LocalArr.DryContactCode"
            @getFormData="setDryContact"
          ></languagePTVue>
          <div v-if="isDry">
            <languagePTVue
              :setName="'Load Management'"
              :columns="LoadMArr"
              :reg="145"
              :setValue="LocalArr.LoadMCode"
              @getFormData="setLoadManagement"
            ></languagePTVue>
            <languagePTVue :setName="'ManualModeControl'" :columns="ManualModeArr" :setValue="LocalArr.ManualModeValue" :reg="133" v-if="isManual"></languagePTVue>
            <div v-if="isSmartSave">
              <pfLockInPointPTVue :setName="'Thresholds on Feedin power'" :setUntil="'W'" :setMin="0" :setMax="8000" :setValue.sync="LocalArr.FeedinOnPowerValue" :reg="134"></pfLockInPointPTVue>
              <pfLockInPointPTVue :setName="'Thresholds off Consumption'" :setUntil="'W'" :setMin="0" :setMax="8000" :setValue.sync="LocalArr.ConsumeOffPowerValue" :reg="136"></pfLockInPointPTVue>
              <pfLockInPointPTVue :setName="'Thresholds off Battery Soc'" :setUntil="'%'" :setMin="0" :setMax="100" :setValue.sync="LocalArr.SwitchOffSocValue" :reg="137"></pfLockInPointPTVue>
              <pfLockInPointPTVue :setName="'MinimumPerOnSignal'" :setUntil="'M'" :setMin="5" :setMax="100" :setValue.sync="LocalArr.MinimumPerOnSignalValue" :reg="138"></pfLockInPointPTVue>
              <pfLockInPointPTVue :setName="'MaximumPerDay'" :setUntil="'M'" :setMin="5" :setMax="1200" :setValue.sync="LocalArr.MiaximumPerDayOnValue" :reg="139"></pfLockInPointPTVue>
              <languagePTVue
                :setName="'Schedule'"
                :columns="backupModeArr"
                :reg="140"
                :setValue="LocalArr.ScheduleCode"
                @getFormData="setSchedule"
              ></languagePTVue>
              <div v-if="isSchedule">
                <chargeTimePTVue :setName="'Work Start Time1'" :setValue="LocalArr.WorkStartTime1" :reg="141"></chargeTimePTVue>
                <chargeTimePTVue :setName="'Work End Time1'" :setValue="LocalArr.WorkEndTime1" :reg="142"></chargeTimePTVue>
                <chargeTimePTVue :setName="'Work Start Time2'" :setValue="LocalArr.WorkStartTime2" :reg="143"></chargeTimePTVue>
                <chargeTimePTVue :setName="'Work End Time2'" :setValue="LocalArr.WorkEndTime2" :reg="144"></chargeTimePTVue>
              </div>
            </div>
          </div>
        </van-collapse-item>
        <van-collapse-item :title="$t('UserPassword')" name="9">
          <newPasswordPTVue :setValue.sync="LocalArr.NewPasswordValue" :reg="115"></newPasswordPTVue>
        </van-collapse-item>
      </van-collapse>
    </van-pull-refresh>
  </div>
</template>

<script>
import datetimePTVue from '../../../components/DateTime/DateTimePT'
import languagePTVue from '../../../components/Language/LanguagePT'
import pfLockInPointPTVue from '../../../components/PfLockInPoint/PfLockInPointPT'
import chargeTimePTVue from '../../../components/ChargeTime/ChargeTimePT'
import newPasswordPTVue from '../../../components/NewPassword/NewPasswordPT'
import chargeTimePT3Vue from '../../../components/ChargeTime/ChargeTime3PT'

export default {
  components:{
    datetimePTVue,
    languagePTVue,
    pfLockInPointPTVue,
    chargeTimePTVue,
    newPasswordPTVue,
    chargeTimePT3Vue
  },
  data(){
    return {
      setOption:sessionStorage.getItem('setOption'),
      isLoading:false,
      activeNames:[],
      langArr:[this.$t('English')],
      toggleArr:[this.$t('No'),this.$t('Yes')],
      backupModeArr:[this.$t('Disable'),this.$t('Enable')],
      DryContactArr:['Load Management','Generator Control'],
      isChargeAtNight:false,
      isFunction:false,
      isDry:false,
      LoadMArr:[this.$t('Disable'),this.$t('Manual'),'SmartSave'],
      isManual:false,
      isSmartSave:false,
      ManualModeArr:[this.$t('Off'),this.$t('ON')],
      isSchedule:false,
      LocalArr:{
        datetimes:'',
        lang:'',
        EPSMuteValue:'',
        SelfUseMinSocValue:'',
        ChargeAtNightCode:'',
        ChargeNightUpperSocValue:'',
        FeedInMinSocValue:'',
        FeedInChargeNightUpperSocValue:'',
        BackupMinSocValue:'',
        BackupChargeNightUpperSocValue:'',
        ChargeStartTime:'',
        ChargeEndTime:'',
        DisChargeStartTime:'',
        DisChargeEndTime:'',
        FunctionCode:'',
        ChargeStartTime2:'',
        ChargeEndTime2:'',
        DisChargeStartTime2:'',
        DisChargeEndTime2:'',
        DryContactCode:'',
        LoadMCode:'',
        ManualModeValue:'',
        FeedinOnPowerValue:'',
        ConsumeOffPowerValue:'',
        SwitchOffSocValue:'',
        MinimumPerOnSignalValue:'',
        MiaximumPerDayOnValue:'',
        ScheduleCode:'',
        WorkStartTime1:'',
        WorkEndTime1:'',
        WorkStartTime2:'',
        WorkEndTime2:'',
        NewPasswordValue:''
      }
    }
  },
  methods: {
    onClickLeft(){
      if(this.setOption==1){
        this.$router.push({path:'/x3hybridg4set'});
      }else{
        this.$router.push({path:'/x3hybridg4setlist'});
      }
    },
    async getData(){
      this.comm.loadingShow();
      let datas = await this.SETAPI.setGetDataPT(localStorage.getItem('domain'),{'tokenId':localStorage.getItem('tokenId'),'sn':localStorage.getItem('wifiSN')});
      if(datas.result!=null&&datas.result!="tokenId不合法!"){
        let dataArr=datas.result;
        let year=this.comm.bit8high(1,dataArr[26]);
        let month=this.comm.bit8low(1,dataArr[26])>=10?this.comm.bit8low(1,dataArr[26]):"0"+this.comm.bit8low(1,dataArr[26]);
        let day=this.comm.bit8high(1,dataArr[25])>=10?this.comm.bit8high(1,dataArr[25]):"0"+this.comm.bit8high(1,dataArr[25]);
        let hour=this.comm.bit8low(1,dataArr[25])>=10?this.comm.bit8low(1,dataArr[25]):"0"+this.comm.bit8low(1,dataArr[25]);
        let minute=this.comm.bit8high(1,dataArr[24])>=10?this.comm.bit8high(1,dataArr[24]):"0"+this.comm.bit8high(1,dataArr[24]);
        this.LocalArr.datetimes=`${year+2000}-${month}-${day} ${hour}:${minute}`;
        this.LocalArr.lang=dataArr[45];
        this.LocalArr.EPSMuteValue=dataArr[48];
        this.LocalArr.SelfUseMinSocValue=dataArr[28];
        this.LocalArr.ChargeAtNightCode=dataArr[29];
        /*判断用户的选择*/
        if(this.LocalArr.ChargeAtNightCode==1){
          this.isChargeAtNight=true;
        }else{
          this.isChargeAtNight=false;
        }
        this.LocalArr.ChargeNightUpperSocValue=dataArr[30];
        this.LocalArr.FeedInMinSocValue=dataArr[31];
        this.LocalArr.FeedInChargeNightUpperSocValue=dataArr[32];
        this.LocalArr.BackupMinSocValue=dataArr[33];
        this.LocalArr.BackupChargeNightUpperSocValue=dataArr[34];

        let workMode1StartHour=this.comm.bit8low(1,dataArr[36]);
        let workMode1StartMinute=this.comm.bit8high(1,dataArr[36]);
        workMode1StartHour=workMode1StartHour>=10?workMode1StartHour:"0"+workMode1StartHour;
        workMode1StartMinute=workMode1StartMinute>=10?workMode1StartMinute:"0"+workMode1StartMinute;
        this.LocalArr.ChargeStartTime=`${workMode1StartHour}:${workMode1StartMinute}`;

        let workMode1EndHour=this.comm.bit8low(1,dataArr[37]);
        let workMode1EndMinute=this.comm.bit8high(1,dataArr[37]);
        workMode1EndHour=workMode1EndHour>=10?workMode1EndHour:"0"+workMode1EndHour;
        workMode1EndMinute=workMode1EndMinute>=10?workMode1EndMinute:"0"+workMode1EndMinute;
        this.LocalArr.ChargeEndTime=`${workMode1EndHour}:${workMode1EndMinute}`;

        let workMode1DisStartHour=this.comm.bit8low(1,dataArr[38]);
        let workMode1DisStartMinute=this.comm.bit8high(1,dataArr[38]);
        workMode1DisStartHour=workMode1DisStartHour>=10?workMode1DisStartHour:"0"+workMode1DisStartHour;
        workMode1DisStartMinute=workMode1DisStartMinute>=10?workMode1DisStartMinute:"0"+workMode1DisStartMinute;
        this.LocalArr.DisChargeStartTime=`${workMode1DisStartHour}:${workMode1DisStartMinute}`;

        let workMode1DisEndHour=this.comm.bit8low(1,dataArr[39]);
        let workMode1DisEndMinute=this.comm.bit8high(1,dataArr[39]);
        workMode1DisEndHour=workMode1DisEndHour>=10?workMode1DisEndHour:"0"+workMode1DisEndHour;
        workMode1DisEndMinute=workMode1DisEndMinute>=10?workMode1DisEndMinute:"0"+workMode1DisEndMinute;
        this.LocalArr.DisChargeEndTime=`${workMode1DisEndHour}:${workMode1DisEndMinute}`;

        this.LocalArr.FunctionCode=dataArr[40];
        /*判断用户的选择*/
        if(this.LocalArr.FunctionCode==1){
          this.isFunction=true;
        }else{
          this.isFunction=false;
        }

        let workMode2StartHour=this.comm.bit8low(1,dataArr[41]);
        let workMode2StartMinute=this.comm.bit8high(1,dataArr[41]);
        workMode2StartHour=workMode2StartHour>=10?workMode2StartHour:"0"+workMode2StartHour;
        workMode2StartMinute=workMode2StartMinute>=10?workMode2StartMinute:"0"+workMode2StartMinute;
        this.LocalArr.ChargeStartTime2=`${workMode2StartHour}:${workMode2StartMinute}`;

        let workMode2EndHour=this.comm.bit8low(1,dataArr[42]);
        let workMode2EndMinute=this.comm.bit8high(1,dataArr[42]);
        workMode2EndHour=workMode2EndHour>=10?workMode2EndHour:"0"+workMode2EndHour;
        workMode2EndMinute=workMode2EndMinute>=10?workMode2EndMinute:"0"+workMode2EndMinute;
        this.LocalArr.ChargeEndTime2=`${workMode2EndHour}:${workMode2EndMinute}`;

        let workMode2DisStartHour=this.comm.bit8low(1,dataArr[43]);
        let workMode2DisStartMinute=this.comm.bit8high(1,dataArr[43]);
        workMode2DisStartHour=workMode2DisStartHour>=10?workMode2DisStartHour:"0"+workMode2DisStartHour;
        workMode2DisStartMinute=workMode2DisStartMinute>=10?workMode2DisStartMinute:"0"+workMode2DisStartMinute;
        this.LocalArr.DisChargeStartTime2=`${workMode2DisStartHour}:${workMode2DisStartMinute}`;

        let workMode2DisEndHour=this.comm.bit8low(1,dataArr[44]);
        let workMode2DisEndMinute=this.comm.bit8high(1,dataArr[44]);
        workMode2DisEndHour=workMode2DisEndHour>=10?workMode2DisEndHour:"0"+workMode2DisEndHour;
        workMode2DisEndMinute=workMode2DisEndMinute>=10?workMode2DisEndMinute:"0"+workMode2DisEndMinute;
        this.LocalArr.DisChargeEndTime2=`${workMode2DisEndHour}:${workMode2DisEndMinute}`;

        this.LocalArr.DryContactCode=dataArr[131];
        /*判断用户的选择*/
        if(this.LocalArr.DryContactCode==0){
          this.isDry=true;
        }else{
          this.isDry=false;
        }

        this.LocalArr.LoadMCode=dataArr[144];
        /*判断用户的选择*/
        if(this.LocalArr.LoadMCode==1){
          this.isManual=true;
          this.isSmartSave=false;
        }else if(this.LocalArr.LoadMCode==2){
          this.isManual=false;
          this.isSmartSave=true;
        }else{
          this.isManual=false;
          this.isSmartSave=false;
        }

        this.LocalArr.ManualModeValue=dataArr[132];
        this.LocalArr.FeedinOnPowerValue=dataArr[133];
        this.LocalArr.ConsumeOffPowerValue=dataArr[135];
        this.LocalArr.SwitchOffSocValue=dataArr[136];
        this.LocalArr.MinimumPerOnSignalValue=dataArr[137];
        this.LocalArr.MiaximumPerDayOnValue=dataArr[138];

        this.LocalArr.ScheduleCode=dataArr[139];
        /*判断用户的选择*/
        if(this.LocalArr.ScheduleCode==1){
          this.isSchedule=true;
        }else{
          this.isSchedule=false;
        }

        let WorkStartTime1Hour=this.comm.bit8low(1,dataArr[140]);
        let WorkStartTime1Minute=this.comm.bit8high(1,dataArr[140]);
        WorkStartTime1Hour=WorkStartTime1Hour>=10?WorkStartTime1Hour:"0"+WorkStartTime1Hour;
        WorkStartTime1Minute=WorkStartTime1Minute>=10?WorkStartTime1Minute:"0"+WorkStartTime1Minute;
        this.LocalArr.WorkStartTime1=`${WorkStartTime1Hour}:${WorkStartTime1Minute}`;

        let WorkEndTime1Hour=this.comm.bit8low(1,dataArr[141]);
        let WorkEndTime1Minute=this.comm.bit8high(1,dataArr[141]);
        WorkEndTime1Hour=WorkEndTime1Hour>=10?WorkEndTime1Hour:"0"+WorkEndTime1Hour;
        WorkEndTime1Minute=WorkEndTime1Minute>=10?WorkEndTime1Minute:"0"+WorkEndTime1Minute;
        this.LocalArr.WorkEndTime1=`${WorkEndTime1Hour}:${WorkEndTime1Minute}`;

        let WorkStartTime2Hour=this.comm.bit8low(1,dataArr[142]);
        let WorkStartTime2Minute=this.comm.bit8high(1,dataArr[142]);
        WorkStartTime2Hour=WorkStartTime2Hour>=10?WorkStartTime2Hour:"0"+WorkStartTime2Hour;
        WorkStartTime2Minute=WorkStartTime2Minute>=10?WorkStartTime2Minute:"0"+WorkStartTime2Minute;
        this.LocalArr.WorkStartTime2=`${WorkStartTime2Hour}:${WorkStartTime2Minute}`;

        let WorkEndTime2Hour=this.comm.bit8low(1,dataArr[143]);
        let WorkEndTime2Minute=this.comm.bit8high(1,dataArr[143]);
        WorkEndTime2Hour=WorkEndTime2Hour>=10?WorkEndTime2Hour:"0"+WorkEndTime2Hour;
        WorkEndTime2Minute=WorkEndTime2Minute>=10?WorkEndTime2Minute:"0"+WorkEndTime2Minute;
        this.LocalArr.WorkEndTime2=`${WorkEndTime2Hour}:${WorkEndTime2Minute}`;

        this.LocalArr.NewPasswordValue=this.comm.showPwd(dataArr[114]);
      }else{
        this.comm.tipsShow('InvNoResponse');
      }
    },
    //设置ChargeAtNight
    setChargeAtNight(data){
      /*判断用户的选择*/
      if(data==1){
        this.isChargeAtNight=true;
      }else{
        this.isChargeAtNight=false;
      }
    },
    //设置Function
    setFunction(data){
      /*判断用户的选择*/
      if(data==1){
        this.isFunction=true;
      }else{
        this.isFunction=false;
      }
    },
    //设置DryContact
    setDryContact(data){
      /*判断用户的选择*/
      if(data==0){
        this.isDry=true;
      }else{
        this.isDry=false;
      }
    },
    //设置LoadManagement
    setLoadManagement(data){
      /*判断用户的选择*/
      if(data==1){
        this.isManual=true;
        this.isSmartSave=false;
      }else if(data==2){
        this.isManual=false;
        this.isSmartSave=true;
      }else{
        this.isManual=false;
        this.isSmartSave=false;
      }
    },
    //设置Schedule
    setSchedule(data){
      /*判断用户的选择*/
      if(data==1){
        this.isSchedule=true;
      }else{
        this.isSchedule=false;
      }
    },
    onRefresh(){
      this.isLoading = false;
      for(let i in this.LocalArr){
        this.LocalArr[i]='';
      }
      this.getData();
    }
  },
  mounted(){
    this.getData();
  }
}
</script>

<style scoped lang="less">

</style>
